﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConcurrantLoadingLib
{
    public class PriceChangedEventArgs : EventArgs
    {
        public readonly double Change;
        public PriceChangedEventArgs(double change)
        {
            Change = change;
        }
    }    

    public class PriceTicker : IDisposable
    {
        private static PriceTicker _instance;
        public static PriceTicker Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new PriceTicker();

                return _instance;
            }
        }

        public event EventHandler<PriceChangedEventArgs> PriceChanged;
        
        private readonly Timer _timer;
        private readonly Random _random = new Random();

        private PriceTicker()
        {
            _timer = new Timer(TimerCallback, null, 1000, 1000);
        }

        private void TimerCallback(object state)
        {
            if (PriceChanged == null)
                return;

            var random = (_random.NextDouble() - 0.5) * 20; // giving a range of -10.0 to +10.0
            PriceChanged(this, new PriceChangedEventArgs(random));
        }

        public void Dispose()
        {
            if (_timer != null) 
                _timer.Dispose();
        }
    }
}